/*
 * GerenciadorProjetosView.java
 */

package gerenciadorprojetos;

import gerenciadorprojetos.dao.Usuario;
import gerenciadorprojetos.forms.CadastrarProjeto;
import gerenciadorprojetos.forms.CadastrarUsuario;
import gerenciadorprojetos.forms.ConsultarProjetos;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import javax.swing.JDialog;
import javax.swing.JFrame;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import gerenciadorprojetos.services.UsuarioService;
import javax.swing.JOptionPane;

/**
 * The application's main frame.
 */
public class GerenciadorProjetosView extends FrameView {

    public GerenciadorProjetosView(SingleFrameApplication app) {
        super(app);

        initComponents();
        
        jTextPane.setText((new StringBuilder()).append("Bem vindo ao sistema Gerenciador de Projetos! \nO usuario atualmente logado no sistema \351 o ").append(System.getProperty("user.name")).toString());
        context = new ClassPathXmlApplicationContext("spring-servlet.xml");
        UsuarioService uS = (UsuarioService)context.getBean("usuarioService");
        usuarioLogado = uS.getUsuarioLogado();
        
        if (usuarioLogado != null) {
            jTextPane.setText((new StringBuilder()).append(jTextPane.getText()).append("\n\nNome Completo:").append(usuarioLogado.getNomeUsuario()).toString());
        } else {
            JOptionPane.showMessageDialog(null, "O usuario logado não existe na base de dados da aplicação ! Favor contactar o analista responsável !");
            System.exit(0);
        }
        
    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            aboutBox = new GerenciadorProjetosAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);
        }
        GerenciadorProjetosApp.getApplication().show(aboutBox);
    }
    
    @Action
    public void showCriarProjeto()
    {
        if(criarProjeto == null)
        {
            javax.swing.JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            criarProjeto = new CadastrarProjeto(mainFrame, true);
        }
        GerenciadorProjetosApp.getApplication().show(criarProjeto);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane = new javax.swing.JTextPane();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        usuarioMenu = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        projetoMenu = new javax.swing.JMenu();
        cadastrarProjetoMenuItem = new javax.swing.JMenuItem();
        consultarProjetoMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setName("mainPanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getResourceMap(GerenciadorProjetosView.class);
        jTextPane.setBackground(resourceMap.getColor("jTextPane.background")); // NOI18N
        jTextPane.setEditable(false);
        jTextPane.setForeground(resourceMap.getColor("jTextPane.foreground")); // NOI18N
        jTextPane.setText(resourceMap.getString("jTextPane.text")); // NOI18N
        jTextPane.setName("jTextPane"); // NOI18N
        jScrollPane1.setViewportView(jTextPane);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 376, Short.MAX_VALUE)
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE)
                .addContainerGap())
        );

        menuBar.setName("menuBar"); // NOI18N

        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(gerenciadorprojetos.GerenciadorProjetosApp.class).getContext().getActionMap(GerenciadorProjetosView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        usuarioMenu.setText(resourceMap.getString("usuarioMenu.text")); // NOI18N
        usuarioMenu.setName("usuarioMenu"); // NOI18N

        jMenuItem1.setAction(actionMap.get("cadastrarUsuario")); // NOI18N
        jMenuItem1.setText(resourceMap.getString("jMenuItem1.text")); // NOI18N
        jMenuItem1.setName("jMenuItem1"); // NOI18N
        usuarioMenu.add(jMenuItem1);

        menuBar.add(usuarioMenu);

        projetoMenu.setText(resourceMap.getString("projetoMenu.text")); // NOI18N
        projetoMenu.setName("projetoMenu"); // NOI18N

        cadastrarProjetoMenuItem.setAction(actionMap.get("showCriarProjeto")); // NOI18N
        cadastrarProjetoMenuItem.setName("cadastrarProjetoMenuItem"); // NOI18N
        projetoMenu.add(cadastrarProjetoMenuItem);

        consultarProjetoMenuItem.setAction(actionMap.get("showConsultarProjeto")); // NOI18N
        consultarProjetoMenuItem.setName("consultarProjetoMenuItem"); // NOI18N
        projetoMenu.add(consultarProjetoMenuItem);

        menuBar.add(projetoMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents

    @Action
    public void showConsultarProjeto() {
        if(consultarProjeto == null)
        {
            javax.swing.JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            consultarProjeto = new ConsultarProjetos(mainFrame, true);
        }
        GerenciadorProjetosApp.getApplication().show(consultarProjeto);
    }

    @Action
    public void cadastrarUsuario() {
        if(cadastrarUsuario == null)
        {
            javax.swing.JFrame mainFrame = GerenciadorProjetosApp.getApplication().getMainFrame();
            cadastrarUsuario = new CadastrarUsuario(mainFrame, true);
        }
        ((CadastrarUsuario)cadastrarUsuario).inicializarFormulario();
        GerenciadorProjetosApp.getApplication().show(cadastrarUsuario);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JMenuItem cadastrarProjetoMenuItem;
    private javax.swing.JMenuItem consultarProjetoMenuItem;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextPane jTextPane;
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    private javax.swing.JMenu projetoMenu;
    private javax.swing.JMenu usuarioMenu;
    // End of variables declaration//GEN-END:variables

    private JDialog aboutBox;
    private JDialog criarProjeto;
    private JDialog consultarProjeto;
    private JDialog cadastrarUsuario;
    private ApplicationContext context;
    private Usuario usuarioLogado;
}
